package com.zzz.cloud.thrift.provider;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.fastjson.JSONObject;
import com.zzz.cloud.thrift.wrapper.ThriftServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.context.event.EventListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NacosServerMetaDataProvider {

    private final Logger log = LoggerFactory.getLogger(NacosServerMetaDataProvider.class);

    private final ServerInfoProvider serverInfoProvider;

    public NacosServerMetaDataProvider(ServerInfoProvider serverInfoProvider) {
        this.serverInfoProvider = serverInfoProvider;
    }

    /**
     *  将Thrift服务信息写到微服务实例的MetadataMap中，从而间接的实现将Thrift服务发布到Eureka
     * @param instance
     * @param serverPort
     * @param serverContextPath
     * @param managementContextPath
     * @param managementPort
     * @return
     */
    public void get(EurekaInstanceConfigBean instance, NacosDiscoveryProperties nacosDiscoveryProperties, int serverPort, String serverContextPath, String managementContextPath, Integer managementPort) {
        Map<String, String> metadataMap = instance.getMetadataMap();
        if (metadataMap == null) {
            metadataMap = new HashMap<>();
            instance.setMetadataMap(metadataMap);
        }
        int port = this.serverInfoProvider.getPort();
        List<ThriftServiceWrapper> allService = serverInfoProvider.getAllService();
        List<String> serverNameList = allService.stream().map(t -> t.getServiceName() + ":" + port).collect(Collectors.toList());
        String serviceListStr = JSONObject.toJSONString(serverNameList);
        metadataMap.put("server.info", serviceListStr);
        log.info("Registry service to eureka [{}]",serviceListStr);
    }


    @EventListener
    public void onInstancePreRegisteredEvent(
            InstancePreRegisteredEvent instancePreRegisteredEvent) {
        Registration registration = instancePreRegisteredEvent.getRegistration();
        NacosDiscoveryProperties nacosDiscoveryProperties = ((NacosRegistration) registration)
                .getNacosDiscoveryProperties();
        Map<String, String> metadataMap = nacosDiscoveryProperties.getMetadata();
        int port = this.serverInfoProvider.getPort();
        List<ThriftServiceWrapper> allService = serverInfoProvider.getAllService();
        List<String> serverNameList = allService.stream().map(t -> t.getServiceName() + ":" + port).collect(Collectors.toList());
        String serviceListStr = JSONObject.toJSONString(serverNameList);
        metadataMap.put("server.info", serviceListStr);
        log.info("Binding service to nacos {}",serviceListStr);
    }
}
